package com.starleague.common.android;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public abstract class AbstractViewBindableListAdapter<BINDABLE, VIEW extends View>
        extends ArrayAdapter<BINDABLE>
{
    private final LayoutInflater inflater;
    private final int resource;

    public AbstractViewBindableListAdapter(LayoutInflater inflater,
                                           int resource,
                                           List<BINDABLE> bindables)
    {
        super(inflater.getContext(), 0, bindables);
        this.inflater = inflater;
        this.resource = resource;
    }

    @Override
    public View getView(int position,
                        View convertView,
                        ViewGroup parent)
    {
        BINDABLE bindable = getItem(position);

        @SuppressWarnings("unchecked")
        VIEW itemView = (VIEW) ((convertView == null)
                ? this.inflater.inflate(this.resource, parent, false)
                : convertView);

        bind(bindable, itemView);

        return itemView;
    }

    protected abstract void bind(BINDABLE bindable,
                                 VIEW view);
}
